import {Component, Input, OnInit} from '@angular/core';
import {UtilComponent} from '../../interface/util-component.interface';
import Map from 'ol/Map';

declare var saveAs: any;
declare var global: any;
declare var jsPDF: any;

@Component({
    selector: 'kylin-export-map',
    templateUrl: './export-map.component.html',
    styleUrls: ['./export-map.component.less']
})
export class ExportMapComponent implements OnInit, UtilComponent {
    // ---------- 接口实现 begin -------------- //
    hasContent = true;
    map: Map;

    @Input() set data(val) {
        Object.assign(this, val);
    };

    // ---------- 接口实现 end ---------------- //
    format = 'a4';
    resolution = 72;
    formatOptions = [
        {
            label: 'A0 (慢)',
            value: 'a0'
        },
        {
            label: 'A1',
            value: 'a1'
        },
        {
            label: 'A2',
            value: 'a2'
        },
        {
            label: 'A3',
            value: 'a3'
        },
        {
            label: 'A4',
            value: 'a4'
        },
        {
            label: 'A5 (快)',
            value: 'a5'
        },
    ];
    resolutionOptions = [
        {value: 72, label: '72 dpi(快)'},
        {value: 150, label: '150 dpi'},
        {value: 300, label: '300 dpi(慢)'},
    ];
    pngLoading = false;
    pdfLoading = false;

    dims = {
        a0: [1189, 841],
        a1: [841, 594],
        a2: [594, 420],
        a3: [420, 297],
        a4: [297, 210],
        a5: [210, 148]
    };

    constructor() {
    }

    ngOnInit() {
        // TODO 待确认跨域图层在底部，或者说 顶部图层没有source的情况
    }

    cancel(options?: any): any {
    }

    downloadImg() {
        this.pngLoading = true;
        this.map.once('rendercomplete', (event) => {
            const canvas = event.context.canvas;
            if (navigator.msSaveBlob) {
                navigator.msSaveBlob((canvas as any).msToBlob(), 'map.png');
            } else {
                canvas.toBlob(function (blob) {
                    saveAs(blob, 'map.png');
                });
            }
            this.pngLoading = false;
        });
        this.map.renderSync();
    }

    exportPDF() {
        const dim = this.dims[this.format];
        const width = Math.round(dim[0] * this.resolution / 25.4);
        const height = Math.round(dim[1] * this.resolution / 25.4);
        const size = /** @type {module:ol/size~Size} */ (this.map.getSize());
        const extent = this.map.getView().calculateExtent(size);
        this.pdfLoading = true;

        this.map.once('rendercomplete', (event) => {
            const canvas = event.context.canvas;
            const data = canvas.toDataURL('image/jpeg');
            const pdf = new jsPDF('landscape', undefined, this.format);
            pdf.addImage(data, 'JPEG', 0, 0, dim[0], dim[1]);
            pdf.save('map.pdf');
            // Reset original map size
            this.map.setSize(size);
            this.map.getView().fit(extent, {size: size});
            this.pdfLoading = false;
        });
        // Set print size
        const printSize = [width, height];
        this.map.setSize(printSize);
        this.map.getView().fit(extent, {size: printSize});
    }
}
